package com.bootdo.screen.service.impl;

import com.bootdo.screen.domain.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PtDataServiceImpl {

    @Autowired
    private  PtclusterloadServiceImpl ptclusterloadService;

    @Autowired
    private PtclusterserverServiceImpl ptclusterserverService;

    @Autowired
    private  PtfaultmonitorServiceImpl ptfaultmonitorService;

    @Autowired
    private PtnetworkstatusServiceImpl ptnetworkstatusService;

    @Autowired
    private  PtresourceServiceImpl  ptresourceService;

    @Autowired
    private PtservicelistServiceImpl ptservicelistService;


    public  List<Map<String,Object>> getClusterServiceList() {
        List<Map<String,Object>>  list = new ArrayList<>();
        List<PtservicelistDO> pts = ptservicelistService.list(null);

        for(PtservicelistDO pt:pts) {
            Iterator<Map<String, Object>> litor = list.iterator();
            int f = 0;
            while (litor.hasNext()) {
                Map<String, Object> map1 = litor.next();
                if (map1.get("location").equals(pt.getLocation())) {
                    List< Map<String, Object> >  lmap =(List< Map<String, Object> > ) map1.get("serviceList");
                    Map<String, Object> map2 = new HashMap<>();
                    map2.put("service",pt.getService());
                    map2.put("status",pt.getStatus());
                    lmap.add(map2);
                    f = 1;
                    if (!pt.getStatus().equals("正常")) {
                        map1.put("locationStatus",pt.getStatus());
                    }
                    break;
                }

            }
            if (f==0) {
                Map<String, Object> map1 =  new HashMap<>();
                map1.put("location",pt.getLocation());
                List< Map<String, Object> >  lmap = new  ArrayList<>();
                Map<String, Object> map2 = new HashMap<>();
                map2.put("service",pt.getService());
                map2.put("status",pt.getStatus());
                lmap.add(map2);
                map1.put("serviceList",lmap);
                map1.put("locationStatus",pt.getStatus());
                list.add(map1);
            }
        }



        return list;
    }

    public   List<Map<String,Object>>  getClusterNetWorkStatus(){
        Map<String,Object> map = new HashMap <>();
        map.put("limit",20);
        map.put("offset",0);
        map.put("order","desc");
        map.put("sort","datetime");
        List<PtnetworkstatusDO> pts = ptnetworkstatusService.list(map);
        List<Map<String,Object>>  list = new ArrayList<>();
        for(PtnetworkstatusDO pt:pts) {
            Map<String, Object> ptmap = new HashMap<>();
            ptmap.put("in",pt.getInnetwork());
            ptmap.put("out",pt.getOutnetwork());
            ptmap.put("datetime",pt.getDatetime());
            list.add(ptmap);
        }
        return  list;
    }


    public   List<Map<String,Object>>  getClusterResourceUsed(){
        Map<String,Object> map = new HashMap <>();
        map.put("limit",20);
        map.put("offset",0);
        map.put("order","desc");
        map.put("sort","datetime");
        List<PtresourceDO> pts = ptresourceService.list(map);
        List<Map<String,Object>>  list = new ArrayList<>();
        for(PtresourceDO pt:pts) {
            Map<String, Object> ptmap = new HashMap<>();
            ptmap.put("write",pt.getWrite());
            ptmap.put("read",pt.getRead());
            ptmap.put("nodes",pt.getNodes());
            ptmap.put("connection",pt.getConnection());
            ptmap.put("datetime",pt.getDatetime());
            list.add(ptmap);
        }
        return  list;
    }

    public   List<Map<String,Object>>  getClusterFaultMonitor(){
        Map<String,Object> map = new HashMap <>();
        map.put("limit",5);
        map.put("offset",0);
        map.put("order","desc");
        map.put("sort","id");
        List<PtfaultmonitorDO> pts = ptfaultmonitorService.list(map);
        List<Map<String,Object>>  list = new ArrayList<>();
        for(PtfaultmonitorDO pt:pts){
            Map<String,Object> ptmap = new HashMap <>();
            ptmap.put("level",pt.getLevel());
            ptmap.put("location",pt.getLocation());
            ptmap.put("service",pt.getService());
            ptmap.put("message",pt.getMessage());
            ptmap.put("datetime",pt.getDatetime());
            list.add(ptmap);
        }
     return list;

    }




    public Map<String,Object> getClusterServerStatus() {
        List<Map<String,Object>>  list = new ArrayList<>();
        Map<String,Object> map1 = new HashMap <>();
        List<PtclusterserverDO> pts = ptclusterserverService.list(null);
        int total = 0;
        int alert = 0;
        int normal =0;
        for (PtclusterserverDO pt:pts) {
            Map<String,Object> map = new HashMap <>();
            map.put("name",pt.getServer());
            map.put("status",pt.getStatus());
            if (pt.getStatus().equals("正常")) {
                normal = normal +1;
            } else {
                alert = alert +1;
            }
            list.add(map);
        }
        map1.put("servers",list);
        map1.put("已安装组件",alert+normal);
        map1.put("正常组件",normal);
        map1.put("异常组件",alert);

        return map1;
    }

    public List<Map<String,Object>> getClusterLoad() {
        List<Map<String,Object>>  list = new ArrayList<>();
        Map<String,Object> map = new HashMap <>();
        map.put("limit",1);
        map.put("offset",0);
        map.put("order","desc");
        map.put("sort","id");
        List<PtclusterloadDO> pts = ptclusterloadService.list(map);
        PtclusterloadDO pt=pts.get(0);
        Map<String,Object> map1 = new HashMap <>();
        map1.put("name","平台CPU使用率");
        map1.put("value",pt.getCpupercentage());
        Map<String,Object> map2 = new HashMap <>();
        map2.put("name","平台存储使用率");
        map2.put("value",pt.getDfspercentage());
        Map<String,Object> map3 = new HashMap <>();
        map3.put("name","平台物理内存使用率");
        map3.put("value",pt.getMempercentage());
        Map<String,Object> map4 = new HashMap <>();
        map4.put("name","平台应用内存使用率");
        map4.put("value",pt.getHeappercentage());
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
         return  list;
    }

}
